package com.tacuati;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import com.tacuati.content.Content;
import com.tacuati.exceptions.CollectionsFileNotFoundException;
import com.tacuati.ui.ContentTree;
import com.tacuati.ui.ContentTreeModel;
import com.tacuati.ui.MainToolBar;

public class Main extends JFrame {
	private static final long serialVersionUID = 1L;
	private static String kindleRoot = "";

	private Content kindleContent = new Content();
	private ContentTree tree;
	private MainToolBar toolbar;

	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		super("Kindle Collections Organizer");
		setLayout(new BorderLayout());
		setSize(500, 650);

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		tree = new ContentTree(kindleContent);
		toolbar = new MainToolBar(this);
		getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
		getContentPane().add(toolbar, "North");
		
		setVisible(true);
	}

	public static String getKindleRoot() {
		return kindleRoot;
	}

	public static void setKindleRoot(String kindleRoot) {
		Main.kindleRoot = kindleRoot;
	}

	public void loadItems() {
		if (!kindleRoot.equals("")){
			String collectionsFilePath = kindleRoot + "/system/collections.json";
			String documentsPath = kindleRoot + "/documents/";
			try {
				kindleContent.clear();
				((ContentTreeModel) tree.getModel()).reload();
				kindleContent.getContent(collectionsFilePath, documentsPath);
				kindleContent.sort();
				((ContentTreeModel) tree.getModel()).reload();
				toolbar.enableSave();
			} catch (CollectionsFileNotFoundException e) {
				kindleRoot = "";
				toolbar.disableSave();
				JOptionPane.showMessageDialog(this, "Collections file not found at " + collectionsFilePath, "Collections file not found", JOptionPane.ERROR_MESSAGE);
			} catch (Exception e){
				kindleRoot = "";
				toolbar.disableSave();
				e.printStackTrace();
			}
		}
	}
	
	public void save(){
		String collectionsFilePath = kindleRoot + "/system/collections.json";
		String backupCollectionsFilePath = collectionsFilePath + ".backup." + new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date());
		
		try {
			Utils.copyFile(collectionsFilePath, backupCollectionsFilePath);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Could not backup collections file " + collectionsFilePath, "Could not backup collections file", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		try {
			Utils.writeFile(collectionsFilePath, kindleContent.toJson());
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(this, "Could not write collections file at " + collectionsFilePath, "Could not write collections file", JOptionPane.ERROR_MESSAGE);
		}
		JOptionPane.showMessageDialog(this, "Collections file writed successfully. Please reboot your kindle.", "Collections file writed successfully", JOptionPane.INFORMATION_MESSAGE);

	}
	
	public void help(){
		String message = "- With the open button select your kindle root directory. Ex.: /media/Kindle/ or G:\\ \n" +
						 "- Copy/Cut/Drag & drop the collections as you want";
		JOptionPane.showMessageDialog(this, message, "Help", JOptionPane.INFORMATION_MESSAGE);
	}
}
